<template>
	<view style="padding-top: 160rpx;">
		<view class="touxiang" @click="uploadImg(1,avatar,avatar_text)">
			<image :src="avatar_text" mode=""></image>
		</view>
		<view class="gudingdb mainpadding" @click="save">
			<view class="bigbtn">返回</view>
		</view>
	</view>
</template>

<script>
	const httpRequest = require("@/common/httpRequest.js")
	export default {
		data() {
			return {
				avatar: "",
				avatar_text: "",
			}
		},
		onLoad(opt) {
			let data = JSON.parse(decodeURIComponent(opt.data));
			this.avatar = data.avatar
			this.avatar_text = data.avatar_text
		},
		methods: {
			uploadImg(value, upImgs, upImgs_text) {
				let _this = this;
				uni.showActionSheet({
					itemList: ['相机', '相册'],
					success: function(res) {
						// 选择相机走0
						if (res.tapIndex == 0) {
							httpRequest.chooseImage(1, ['camera']).then(res => {
								for (var i = 0; i < res.length; i++) {
									_this.uploadImageService(res[i], upImgs, upImgs_text)
								}
							}).catch(err => {});
						}
						// 选择相册走1
						if (res.tapIndex == 1) {
							httpRequest.chooseImage(1, ['album']).then(res => {
								for (var i = 0; i < res.length; i++) {
									_this.uploadImageService(res[i], upImgs, upImgs_text)
								}
							}).catch(err => {});
						}
					},
					fail: function(res) {}
				});
			},
			uploadImageService(url, upImg, upImgs_text) {
				let _this = this;
				httpRequest.uploadFile('/api/common/upload', url).then(res => {
					let image_item = res.fullurl;
					this.avatar = res.url;
					this.avatar_text = image_item
				}).catch(err => {});
			},
			save(val) {
				let data = {
					avatar:this.avatar,
					avatar_text:this.avatar_text
				}
				uni.$emit("editheadimg", data)
				uni.navigateBack(1)
			}
		}
	}
</script>

<style lang="scss" scoped>
	.touxiang {
		width: 100%;
		height: 752rpx;

		image {
			width: 100%;
			height: 100%;
		}
	}
</style>